#-------------------------------------------------------------------------------
# Name:        firstScript
# Purpose:
#
# Author:      James Royal
#
# Created:     07/12/2010
# Copyright:   (c) James Royal 2010
# Licence:     <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import re
import sys

def isItTime(line):
    if re.search(r'MWF',line):
        return True;
    elif re.search(r'MW',line):
        return True;
    elif re.search(r'^T\s',line):
        return True;
    elif re.search(r'MTWTHF',line):
        return True;
    elif re.search(r'MTWF',line):
        return True;
    elif re.search(r'MWTHF',line):
        return True;
    elif re.search(r'^F\s',line):
        return True;
    elif re.search(r'^TH\s',line):
        return True;
    elif re.search(r'TTH',line):
        return True;
    else:
        return False

def specialCheck(line):
    if re.search(r'^P ',line):
        return True;
    else:
        return False;

def getTime(line):
    if isItTime(line):
        t = re.search(r'MWF',line)
        if t:
            return t.group();
        t = re.search(r'MW',line)
        if t:
            return t.group();
        t = re.search(r'^T\s',line)
        if t:
            return t.group();
        t = re.search(r'MTWTHF',line)
        if t:
            return t.group()
        t = re.search(r'MTWF',line)
        if t:
            return t.group();
        t = re.search(r'MWTHF',line)
        if t:
            return t.group();
        t = re.search(r'^F\s',line)
        if t:
            return t.group();
        t = re.search(r'^TH\s',line)
        if t:
            return t.group();
        t = re.search(r'TTH',line)
        if t:
            return t.group();
        else:
            return False
def main():
    file = open("test.txt")
    fileWriter =open("testWrite.txt" , "w")
    name = "";
    for line in file:

        #match the class ID: ex. C S 307
        match = re.search(r'[A-Z]\s?[A-Z][A-Z]? \d\d\d[A-Z]?',line)
        if match:
            name = match.group()
            if not isItTime(name):
                fileWriter.write("\n")
                fileWriter.writelines(name)
                fileWriter.write("\n")

        #match the class description: ex. Logic Sets and Functions
        matchName = re.search(r'([A-Z+][:]?\s?)+',line)
        if matchName:
            name = matchName.group()
            if not len(name) == 1:
                if not isItTime(name):
                    if not match:
                        if not specialCheck(name):
                            fileWriter.writelines(name)
                            fileWriter.write("\n")

        #match the unique number
        matchUnique = re.search(r'\d{5}',line)
        if matchUnique:
            name = matchUnique.group()
            fileWriter.writelines(name)

        #match the times
        matchTime = re.search(r'\s?\d\d\d\d? -\d\d\d\d?\w?',line)
        if matchTime:
            name = matchTime.group()
            fileWriter.writelines(name)

        #match the location
        matchLoc = re.search(r'\s[A-Z][A-Z][A-Z]\s',line)
        if matchLoc:
            name = matchLoc.group()
            if not isItTime(name):
                fileWriter.writelines(name)

        #match the Professor
        matchProf = re.search(r'[A-Z]+, [A-Z]',line)
        if matchProf:
            name = matchProf.group()
            fileWriter.writelines(name)
            fileWriter.write("\n")


    file.close()
    fileWriter.close()

if __name__ == '__main__':
    main()
